﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Web.Profile;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DAO;

namespace HRProject.User
{
    public partial class ViewUserInfo : System.Web.UI.Page
    {
        private List<BLRecordType> recordTypes;
        private List<BLRecord> records;

        public List<BLRecord> Records
        {
            get { return records; }
            set { records = value; }
        }

        public List<BLRecordType> RecordTypes
        {
            get { return recordTypes; }
            set { recordTypes = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RecordTypeDAO dao = new RecordTypeDAO();
            BLRecordType filter = new BLRecordType();
            filter.Id = Guid.Empty;
            filter.Name = string.Empty;
            recordTypes = dao.Search(filter);
            recordTypes.Insert(0,filter);

            records = new List<BLRecord>();

            Guid userId = Guid.Empty;
            try
            {
                userId = new Guid(Request.Params["editUserId"]);
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
            MembershipUser mUser = Membership.GetUser(userId);
            if (mUser != null)
            {
                if (BLAuthentication.IsEditAllowed || (Membership.GetUser() != null && Membership.GetUser().ProviderUserKey.Equals(userId)))
                {
                    BLUser user = new BLUser(mUser);
                    if (!IsPostBack)
                    {
                        lblUserNameInfo.Text = user.UserName;
                        tbxEmail.Text = user.Email;
                        tbxFullName.Text = user.FullName;
                        tbxIdentifierSerial.Text = user.IdentifiedSerial;
                        tbxPhone.Text = user.Phone;
                        tbxContact.Text = user.Contact;
                        tbxAddress.Text = user.Address;
                        tbxBirthDate.Text = user.BirthDate.ToString("dd/MM/yyyy");
                        tbxBirthPlace.Text = user.BirthPlace;
                        tbxMaritalStatus.Text = user.MaritalStatus;
                        tbxPeople.Text = user.People;
                        tbxAddress2.Text = user.ExtraContact;
                        txaAssets.Text = user.Assets;
                        divExtraDescription.InnerText = user.ExtraDescription;
                        tbxGender.Text = BLGender.GetName(user.Gender);
                    }

                    RecordDAO recordDAO = new RecordDAO();
                    BLRecord recFilter = new BLRecord();
                    recFilter.Id = Guid.Empty;
                    recFilter.Name = string.Empty;
                    recFilter.UserId = userId;
                    recFilter.Type = new BLRecordType();
                    recFilter.Id = Guid.Empty;
                    records = recordDAO.search(recFilter);
                }
                else
                {
                    Response.Redirect("/LogIn.aspx");
                }
            }
            else
            {
                Response.Redirect("/LogIn.aspx");
            }
        }
    }
}
